<script>
function func_setListLibId(scheId, checkBoxId){
	var listId = $("#hidListLibId").val();
	
	if(document.getElementById(checkBoxId).checked ==true){
		if(listId == ''){
			listId = listId + scheId;
		}else{
			listId = listId + "," + scheId;
		}
	} else {
		if (listId.indexOf(",") > 0){
			var n=listId.split(",");
			var index = n.indexOf(scheId);

			if (index >= 0){
				n.splice(index, 1);
			}
			
		    listId = '';
			for (var i = 0; i < n.length; i++){
				
				if(listId == ''){
					listId = listId + n[i];
				}else{
					listId = listId + "," + n[i];
				} 
			}	
		} else {
			if (scheId.indexOf(listId) >= 0) {
				listId = '';
			}
		}
	}
	$("#hidListLibId").val(listId);
}

function libAdd(subId, subName){
	$("#hidSubId").val(subId);
	$("#hidSubName").val(subName);
	$("#mainForm").prop("action", "/it_center/Libs/addLib");
	return $("#mainForm").submit();
}

function lib_delete(subId, subName){
	$("#hidSubId").val(subId);
	$("#hidSubName").val(subName);
	return $("#mainForm").submit();
}

function libEdit(subId, subName, libId){
	$("#hidListLibId").val(libId);
	$("#hidSubId").val(subId);
	$("#hidSubName").val(subName);
	$("#mainForm").prop("action", "/it_center/Libs/edit");
	return $("#mainForm").submit();
}

</script>

<form id="mainForm" method="post" action="/it_center/Libs/libDelete">	
	<input type="hidden" name="libId" id="hidListLibId"/>
	<input type="hidden" name="subId" id="hidSubId"/>
	<input type="hidden" name="subName" id="hidSubName"/>
</form>
	
<section id="content" class="cols-a">
	<section class="news-a">
		<?php if (!isset($listLibs)){?>
			<header>
				<h2>Danh sách các thư viện </h2>
				<article>
				<header>
					<h3>Vui lòng chọn môn học tại mục danh sách môn học.</h3>
				</header>
				</article>
			</header>
		<?php }else{?>
			<?php If (count($listLibs) > 0){?>
				<header>
					<h2>Danh sách các thư viện môn <?php echo $selectedSubName;?></h2>
					<p>(Click vào tên thư viện để thay đổi)</p>
					<a onclick="libAdd(<?php echo $selectedSubId;?>, '<?php echo $selectedSubName;?>')" class="regist_min" >Thêm thư viện</a>
					<a class="delete" onclick="lib_delete(<?php echo $selectedSubId;?>, '<?php echo $selectedSubName;?>')">Xóa thư viện</a>
				</header>
				<?php foreach ($listLibs as $lib):?>
					<article>
						<header>
							<h3><input type="checkbox" id="check<?php echo $lib['Lib']['m_lib_id'];?>" onclick="func_setListLibId('<?php echo $lib['Lib']['m_lib_id'];?>', 'check<?php echo $lib['Lib']['m_lib_id'];?>')">
							<a onclick="libEdit(<?php echo $selectedSubId;?>, '<?php echo $selectedSubName;?>', <?php echo $lib['Lib']['m_lib_id'];?>)"><?php echo $lib['Lib']['m_lib_title']?></a></h3>
						</header>
						<p>
						File: <?php echo $lib['Lib']['m_file_name']; ?> 
						<br/>
						Mô tả: <?php echo $lib['Lib']['m_lib_descript']; ?>
						</p>
					</article>
				<?php endforeach; ?>
				<?php if ($pagerInfo['total_page'] > 1){ ?>
				<footer>
					<nav class="pagination-a">
						<?php if ($pagerInfo['start_page'] > 1){?>
							<p><a href="/it_center/Libs/list_lib?sub_id=<?php echo $selectedSubId;?>&sub_name=<?php echo $selectedSubName;?>&page_no=<?php echo $pagerInfo['page_no'] - 1;?>&start_page=<?php echo $pagerInfo['start_page'];?>&end_page=<?php echo $pagerInfo['end_page'];?>">Previous</a></p>
						<?php }?>
						<ol>
							<?php for($i = $pagerInfo['start_page']; $i <= $pagerInfo['end_page']; $i++){?>
								<li <?php if ($i = $pagerInfo['page_no']){?>class="active"<?php }?>><a href="/it_center/Libs/list_lib?sub_id=<?php echo $selectedSubId;?>&sub_name=<?php echo $selectedSubName;?>&page_no=<?php echo $i;?>&start_page=<?php echo $pagerInfo['start_page'];?>&end_page=<?php echo $pagerInfo['end_page'];?>"><?php echo $i;?></a></li>
							<?php }?>
						</ol>
						<?php if ($pagerInfo['end_page'] < $pagerInfo['total_page'] - 4){?>
							<p><a href="/it_center/Libs/list_lib?sub_id=<?php echo $selectedSubId;?>&sub_name=<?php echo $selectedSubName;?>&page_no=<?php echo $pagerInfo['page_no'] + 1;?>&start_page=<?php echo $pagerInfo['start_page'];?>&end_page=<?php echo $pagerInfo['end_page'];?>">Next</a></p>
						<?php }?>
					</nav>
				</footer>
				<?php }?>
			<?php } Else {?>
				<header>
					<h2>Danh sách các thư viện môn <?php echo $selectedSubName?></h2>
				</header>
				<article>
					<header>
						<h3>Hiện tại chưa có thư viện cho môn học này.</h3>
						<a onclick="libAdd(<?php echo $selectedSubId;?>, '<?php echo $selectedSubName;?>')" class="regist_min" >Thêm thư viện</a>
					</header>
					</article>
			<?php }?>
			<?php }?>
		
	</section>
	<aside>
		<h3>Danh Sách Môn Học</h3>
		<nav>
			<ul>
				<?php foreach ($subjects as $subject):?>						
				<li><a href="/it_center/Libs/list_lib?sub_id=<?php echo $subject['Subject']['sub_id'];?>&sub_name=<?php echo $subject['Subject']['sub_name'];?>"><?php echo $subject['Subject']['sub_name'];?></a></li>
				<?php endforeach;?>
			</ul>
		</nav>
	</aside>
</section>